New network-bridge script and associated gubbins.
authoremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Wed, 19 Oct 2005 15:24:54 +0000 (16:24 +0100)
committeremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Wed, 19 Oct 2005 15:24:54 +0000 (16:24 +0100)
This is Kurt Garloff's reworked network-bridge script:

* we got rid of ifconfig
* it works for netdev != eth0
* arp on and off are symmetric as are ifdown and ifup
* ifup will be passed the ifcfg config file name if needed
  (the ifup may otherwise figure that the veth0 hardware is
   NOT the same as the original ${netdev} and not use the same
   config -- this happens on SUSE. Charles Coffing tracked this
   one down.)

Plus Kurt's avoid-dash patch:

the network setup scripts on SUSE have trouble with the bridge
name xen-br0; they don't expect the '-'.
Arguably this should be fixed.
But I assume there's more scripts out there which may not like it,
so I suggest the following patch to rename xen-br0 to xenbr0.

Plus Charles Duffy's patch to support multiple bridges:

The attached patch allows the network-bridge script to be used to
generate multiple bridges corresponding to different physical
interfaces. It adds a new parameter, "vifnum", used to refer both to
the loopback interface to be used and to set defaults regarding the
physical interface and bridge name.

Thus, if one wishes to start xenbr0 on eth0 and xenbr1 on eth1, one
need only call:

network-bridge start ## vifnum is 0 by default
network-bridge start vifnum=1

...well, that and set loopback.nloopbacks=2 in the Dom0 kernel
parameters.

Plus renaming of virtnum to vifnum in Charles' patch, as requested by Ian Pratt.

Plus a fix to DevController to allocate vif IDs starting from 0 (i.e. vif2.0
is now domain 2's first vif, as opposed to vif2.1 in the recent past).

Plus tidying up inside network-bridge using some helper variables.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
12 files changed:
tools/examples/network-bridge [changed mode: 0755->0644]
tools/examples/vif-bridge
tools/examples/xend-config.sxp
tools/examples/xmexample.vmx
tools/examples/xmexample1
tools/examples/xmexample2
tools/ioemu/target-i386-dm/qemu-ifup
tools/misc/netfix
tools/python/xen/xend/XendRoot.py
tools/python/xen/xend/server/DevController.py
tools/python/xen/xend/server/SrvDaemon.py
tools/vnet/vnet-module/varp.c

old mode 100755 (executable)
new mode 100644 (file)
index f0e1e16..c6fb1c1
@@ -5,8 +5,8 @@
 # The script name to use is defined in /etc/xen/xend-config.sxp
 # in the network-script field.
 #
-# This script creates a bridge (default xen-br0), adds a device
-# (default eth0) to it, copies the IP addresses from the device
+# This script creates a bridge (default xenbr${vifnum}), adds a device
+# (default eth${vifnum}) to it, copies the IP addresses from the device
 # to the bridge and adjusts the routes accordingly.
 #
 # If all goes well, this should ensure that networking stays up.
 #
 # Vars:
 #
-# bridge     The bridge to use (default xen-br0).
-# netdev     The interface to add to the bridge (default eth0).
-# antispoof  Whether to use iptables to prevent spoofing (default yes).
+# vifnum     Virtual device number to use (default 0). Numbers >=1
+#            require the netback driver to have nloopbacks set to a
+#            higher value than its default of 1.
+# bridge     The bridge to use (default xenbr${vifnum}).
+# netdev     The interface to add to the bridge (default eth${vifnum}).
+# antispoof  Whether to use iptables to prevent spoofing (default no).
 #
 # start:
 # Creates the bridge and enslaves netdev to it.
@@ -60,11 +63,37 @@ shift
 # Pull variables in args in to environment.
 for arg ; do export "${arg}" ; done
 
-bridge=${bridge:-xen-br0}
-netdev=${netdev:-eth0}
+vifnum=${vifnum:-0}
+bridge=${bridge:-xenbr${vifnum}}
+netdev=${netdev:-eth${vifnum}}
 antispoof=${antispoof:-no}
 
-echo "*network $OP bridge=$bridge netdev=$netdev antispoof=$antispoof" >&2
+pdev="p${netdev}"
+vdev="veth${vifnum}"
+vif0="vif0.${vifnum}"
+
+echo "*network $OP bridge=$bridge netdev=$netdev antispoof=$antispoof vifnum=$vifnum" >&2
+
+legacy_mask_to_prefix() {
+    mask=$1
+    first=${mask%%.*}
+    second=${mask#*.}
+    third=${second#*.}
+    fourth=${third#*.}
+    second=${second%%.*}
+    third=${third%%.*}
+    declare -i INT FULLMASK BIT
+    INT=$((((($first*256)+$second)*256+$third)*256+$fourth))
+    FULLMASK=4294967295
+    BIT=1
+    for bit in `seq 32 -1 0`; do
+       if test $FULLMASK -eq $INT; then PREFIX=$bit; return; fi
+       FULLMASK=$(($FULLMASK-$BIT))
+       BIT=$((BIT*2))
+    done
+    echo "ERROR converting netmask $mask to prefix"
+    exit 1
+}
 
 # Usage: transfer_addrs src dst
 # Copy all IP addresses (including aliases) from device $src to device $dst.
@@ -99,6 +128,7 @@ s/inet/ip addr del/
 s@\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\)/[0-9]\+@\1@
 s/${src}/dev ${src}/
 " | sh -e
+    ip link set dev ${dst} up
 }
 
 # Usage: transfer_routes src dst
@@ -131,11 +161,11 @@ create_bridge () {
 
     # Don't create the bridge if it already exists.
     if ! brctl show | grep -q ${bridge} ; then
-        brctl addbr ${bridge}
-        brctl stp ${bridge} off
-        brctl setfd ${bridge} 0
+       brctl addbr ${bridge}
+       brctl stp ${bridge} off
+       brctl setfd ${bridge} 0
     fi
-    ifconfig ${bridge} up
+    ip link set ${bridge} up
 }
 
 # Usage: add_to_bridge bridge dev
@@ -144,7 +174,7 @@ add_to_bridge () {
     local dev=$2
     # Don't add $dev to $bridge if it's already on a bridge.
     if ! brctl show | grep -q ${dev} ; then
-        brctl addif ${bridge} ${dev}
+       brctl addif ${bridge} ${dev}
     fi
 }
 
@@ -166,8 +196,10 @@ show_status () {
     local bridge=$2
     
     echo '============================================================'
-    ifconfig ${dev}
-    ifconfig ${bridge}
+    ip addr show ${dev}
+    ip addr show ${bridge}
+    echo ' '
+    brctl show ${bridge}
     echo ' '
     ip route list
     echo ' '
@@ -177,100 +209,113 @@ show_status () {
 
 op_start () {
     if [ "${bridge}" == "null" ] ; then
-        return
+       return
     fi
 
     create_bridge ${bridge}
 
-    if ifconfig 2>/dev/null | grep -q veth0 ; then
-        return
+    if ! ip link show 2>/dev/null | grep -q "^[0-9]*: ${vdev}"; then
+       return
     fi
 
-    if ifconfig veth0 2>/dev/null | grep -q veth0 ; then
-       mac=`ifconfig ${netdev} | grep HWadd | sed -e 's/.*\(..:..:..:..:..:..\).*/\1/'`
-       if ! ifdown ${netdev} ; then
-               # if ifup didn't work, see if we have an ip= on cmd line
-               if egrep 'ip=[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:' /proc/cmdline ; 
-               then
-                        kip=`sed -e 's!.*ip=\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\):.*!\1!' /proc/cmdline`
-                        kmask=`sed -e 's!.*ip=[^:]*:[^:]*:[^:]*:\([^:]*\):.*!\1!' /proc/cmdline` 
-                        kgate=`sed -e 's!.*ip=[^:]*:[^:]*:\([^:]*\):.*!\1!' /proc/cmdline`
-                       ifconfig ${netdev} 0.0.0.0 down
-               fi
+    if ip link show ${vdev} 2>/dev/null >/dev/null; then
+       mac=`ip link show ${netdev} | grep 'link\/ether' | sed -e 's/.*ether \(..:..:..:..:..:..\).*/\1/'`
+       eval `/sbin/getcfg -d /etc/sysconfig/network/ -f ifcfg- -- ${netdev}`
+       transfer_addrs ${netdev} ${vdev}
+       if ! ifdown ${netdev}; then
+           # if ifup didn't work, see if we have an ip= on cmd line
+           if egrep 'ip=[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:' /proc/cmdline; then
+               kip=`sed -e 's!.*ip=\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\):.*!\1!' /proc/cmdline`
+               kmask=`sed -e 's!.*ip=[^:]*:[^:]*:[^:]*:\([^:]*\):.*!\1!' /proc/cmdline` 
+               kgate=`sed -e 's!.*ip=[^:]*:[^:]*:\([^:]*\):.*!\1!' /proc/cmdline`
+               ip link set ${netdev} down
+               ip addr flush ${netdev}
+           fi
        fi
-       ip link set ${netdev} name p${netdev}
-       ip link set veth0 name ${netdev}
-       ifconfig p${netdev} 0.0.0.0 -arp down
-       ifconfig p${netdev} hw ether fe:ff:ff:ff:ff:ff
-       ifconfig ${netdev} hw ether ${mac}
-       add_to_bridge ${bridge} vif0.0
-       add_to_bridge ${bridge} p${netdev}
+       ip link set ${netdev} name ${pdev}
+       ip link set ${vdev} name ${netdev}
+       ip link set ${pdev} down arp off
+       ip link set ${pdev} addr fe:ff:ff:ff:ff:ff
+       ip addr flush ${pdev}
+       ip link set ${netdev} addr ${mac} arp on
+       add_to_bridge ${bridge} ${vif0}
+       add_to_bridge ${bridge} ${pdev}
        ip link set ${bridge} up
-       ip link set vif0.0 up
-       ip link set p${netdev} up
-       if ! ifup ${netdev} ; then
-               if [ ${kip} ] ; then
-                       # use the addresses we grocked from /proc/cmdline       
-                       ifconfig ${netdev} ${kip} 
-                       [ ${kmask} ] && ifconfig ${netdev} netmask ${kmask} 
-                       ifconfig ${netdev} up
-                       [ ${kgate} ] && ip route add default via ${kgate}       
+       ip link set ${vif0} up
+       ip link set ${pdev} up 
+       if ! ifup ${HWD_CONFIG_0} ${netdev} ; then
+           if [ ${kip} ] ; then
+               # use the addresses we grocked from /proc/cmdline
+               if [ -z "${kmask}" ]; then 
+                   PREFIX=32; 
+               else 
+                   legacy_to_prefix ${kmask};
                fi
-        fi
+               ip addr add ${kip}/${PREFIX} dev ${netdev}
+               ip link set dev ${netdev} up
+               [ ${kgate} ] && ip route add default via ${kgate}
+           fi
+       fi
     else
-       # old style without veth0
-       transfer_addrs ${netdev} ${bridge}
-        transfer_routes ${netdev} ${bridge}
+       # old style without ${vdev}
+       transfer_addrs  ${netdev} ${bridge}
+       transfer_routes ${netdev} ${bridge}
     fi
-    
+
     if [ ${antispoof} == 'yes' ] ; then
-        antispoofing ${netdev} ${bridge}
+       antispoofing ${netdev} ${bridge}
     fi
 }
 
 op_stop () {
-    if [ "${bridge}" == "null" ] ; then
-        return
+    if [ "${bridge}" == "null" ]; then
+       return
+    fi
+    if ! ip link show ${bridge} >/dev/null 2>&1; then
+       return
     fi
 
-    if ifconfig peth0 2>/dev/null | grep -q peth0 ; then
-
-        ifconfig vif0.0 down
-        mac=`ifconfig eth0 | grep HWadd | \
-            sed -e 's/.*\(..:..:..:..:..:..\).*/\1/'`
-        ifconfig ${netdev} 0.0.0.0 down
-        ifconfig ${netdev} hw ether fe:ff:ff:ff:ff:ff
+    if ip link show ${pdev} 2>/dev/null >/dev/null; then
+       ip link set dev ${vif0} down
+       mac=`ip link show ${netdev} | grep 'link\/ether' | sed -e 's/.*ether \(..:..:..:..:..:..\).*/\1/'`
+       transfer_addrs ${netdev} ${pdev}
+       ifdown ${netdev}
+       ip link set ${netdev} down arp off
+       ip link set ${netdev} addr fe:ff:ff:ff:ff:ff
+       ip link set ${pdev} down
+       ip addr flush ${netdev}
+       ip link set ${pdev} addr ${mac} arp on
 
-        ifconfig p${netdev} down
-        ifconfig p${netdev} hw ether ${mac} arp 
-        brctl delif ${bridge} p${netdev}
+       brctl delif ${bridge} ${pdev}
+       brctl delif ${bridge} ${vif0}
+       ip link set ${bridge} down
 
-        ip link set eth0 name veth0
-        ip link set peth0 name eth0
-        ifconfig ${bridge} down
-        brctl delbr ${bridge}
-        ifup eth0
+       ip link set ${netdev} name ${vdev}
+       ip link set ${pdev} name ${netdev}
+       ifup ${netdev}
 
     else
-        transfer_routes ${bridge} ${netdev}
+       transfer_routes ${bridge} ${netdev}
+       ip link set ${bridge} down
     fi
+    brctl delbr ${bridge}
 }
 
 case ${OP} in
     start)
-        op_start
-        ;;
+       op_start
+       ;;
     
     stop)
-        op_stop
-        ;;
+       op_stop
+       ;;
 
     status)
-        show_status ${netdev} ${bridge}
-       ;;
+       show_status ${netdev} ${bridge}
+       ;;
 
     *)
-       echo 'Unknown command: ' ${OP} >&2
-       echo 'Valid commands are: start, stop, status' >&2
-       exit 1
+       echo 'Unknown command: ' ${OP} >&2
+       echo 'Valid commands are: start, stop, status' >&2
+       exit 1
 esac
index 0174d8111615c8a2b0d44aad87c1dfe1b0af1727..4a947397f469b1f7132245767972946b26d04281 100755 (executable)
@@ -8,7 +8,7 @@
 #
 # Example invocation:
 #
-# vif-bridge up domain=VM1 vif=vif1.0 bridge=xen-br0 ip="128.232.38.45/28 10.10.10.55/24"
+# vif-bridge up domain=VM1 vif=vif1.0 bridge=xenbr0 ip="128.232.38.45/28 10.10.10.55/24"
 #
 #
 # Usage:
index 75152f21db519e9a1524515f5acadafeeb4f0452..881eaacfbc03e09c1720104a1b64800208e52994 100644 (file)
@@ -33,7 +33,7 @@
 # The script used to start/stop networking for xend.
 (network-script    network-bridge)
 # The default bridge that virtual interfaces should be connected to.
-(vif-bridge        xen-br0)
+(vif-bridge        xenbr0)
 # The default script used to control virtual interfaces.
 (vif-script        vif-bridge)
 
index a15ad321a95490d1c362647c8a795362babc2b8e..f9665ce67d9d7fcad571de9195e198694ecc68a3 100644 (file)
@@ -35,7 +35,7 @@ vcpus=1
 
 # Optionally define mac and/or bridge for the network interfaces.
 # Random MACs are assigned if not given.
-#vif = [ 'mac=aa:00:00:00:00:11, bridge=xen-br0' ]
+#vif = [ 'mac=aa:00:00:00:00:11, bridge=xenbr0' ]
 
 #----------------------------------------------------------------------------
 # Define the disk devices you want the domain to have access to, and
index be3af30a952466201955a9e527c1c111916a0620..cd73c7c3fafc0e2dc972d44dbd928a45d8505569 100644 (file)
@@ -36,7 +36,7 @@ name = "ExampleDomain"
 
 # Optionally define mac and/or bridge for the network interfaces.
 # Random MACs are assigned if not given.
-#vif = [ 'mac=aa:00:00:00:00:11, bridge=xen-br0' ]
+#vif = [ 'mac=aa:00:00:00:00:11, bridge=xenbr0' ]
 
 #----------------------------------------------------------------------------
 # Define the disk devices you want the domain to have access to, and
index 8272e84ab504daadc22d84ed458ebb1c1f36b9fd..ba89eef7eb1fbd9626be9220e8c1464585c246f8 100644 (file)
@@ -67,7 +67,7 @@ vcpus = 4 # make your domain a 4-way
 
 # Optionally define mac and/or bridge for the network interfaces.
 # Random MACs are assigned if not given.
-#vif = [ 'mac=aa:00:00:00:00:11, bridge=xen-br0' ]
+#vif = [ 'mac=aa:00:00:00:00:11, bridge=xenbr0' ]
 
 #----------------------------------------------------------------------------
 # Define the disk devices you want the domain to have access to, and
index 87bff3410c0d17e77f8e3ae1440a5f9aa940d107..a23f1d2b685c2942aa48f66c34faaea5cfdf95bc 100755 (executable)
@@ -7,4 +7,4 @@ echo 'config qemu network with xen bridge for '
 echo $*
 
 ifconfig $1 0.0.0.0 up
-brctl addif xen-br0 $1
+brctl addif xenbr0 $1
index e18923f1320099a4b6cce7a0b6c4367a575bdd0f..669a95497abb4a723aeee131120f4483b454b211 100644 (file)
@@ -3,7 +3,7 @@
 #============================================================================
 # Copyright (C) 2004 Mike Wray <mike.wray@hp.com>
 #============================================================================
-# Move the IP address from eth0 onto the Xen bridge (xen-br0).
+# Move the IP address from eth0 onto the Xen bridge (xenbr0).
 # Only works if the bridge control utils (brctl) have been installed.
 #============================================================================
 
@@ -19,7 +19,7 @@ long_options  = ['help', 'verbose', 'quiet',
                  'interface=', 'bridge=', 'create']
 
 defaults['interface'] = 'eth0'
-defaults['bridge'] = 'xen-br0'
+defaults['bridge'] = 'xenbr0'
 
 def usage():
     print """Usage:
index e88e8ff4b32c5329ccc3c5b5bebdfa112316a3ef..d5d348d94b45b73ea93c3c627df146d2bb968a1e 100644 (file)
@@ -253,7 +253,7 @@ class XendRoot:
         return self.get_config_bool('enable-dump', 'no')
 
     def get_vif_bridge(self):
-        return self.get_config_value('vif-bridge', 'xen-br0')
+        return self.get_config_value('vif-bridge', 'xenbr0')
 
     def get_vif_script(self):
         return self.get_config_value('vif-script', 'vif-bridge')
index 8141e890b1a4cbd36534b057be2ee660f1628213..0060a687fd0d3791cefd26d99965eea068ef22e7 100644 (file)
@@ -160,7 +160,7 @@ class DevController:
                 if result:
                     result = int(result)
                 else:
-                    result = 1
+                    result = 0
                 t.write("nextDeviceID", str(result + 1))
                 if t.commit():
                     return result
index 543502ad958c2687b847b7fabccca0f8fdf6ae80..2bfcce85a79defd84b2de826d25da0b98a975946 100644 (file)
@@ -32,34 +32,6 @@ class Daemon:
         self.traceindent = 0
         self.child = 0 
         
-    def daemon_pids(self):
-        pids = []
-        pidex = '(?P<pid>\d+)'
-        pythonex = '(?P<python>\S*python\S*)'
-        cmdex = '(?P<cmd>.*)'
-        procre = re.compile('^\s*' + pidex + '\s*' + pythonex + '\s*' + cmdex + '$')
-        xendre = re.compile('^\S+/xend\s*(start|restart)\s*.*$')
-        procs = os.popen('ps -e -o pid,args 2>/dev/null')
-        for proc in procs:
-            pm = procre.match(proc)
-            if not pm: continue
-            xm = xendre.match(pm.group('cmd'))
-            if not xm: continue
-            pids.append(int(pm.group('pid')))
-        return pids
-
-    def new_cleanup(self, kill=0):
-        err = 0
-        pids = self.daemon_pids()
-        if kill:
-            for pid in pids:
-                print "Killing daemon pid=%d" % pid
-                os.kill(pid, signal.SIGHUP)
-        elif pids:
-            err = 1
-            print "Daemon already running: ", pids
-        return err
-
     def read_pid(self, pidfile):
         """Read process id from a file.
 
@@ -115,12 +87,9 @@ class Daemon:
             os.remove(pidfile)
         return running
 
-    def cleanup_xend(self, kill=False):
+    def cleanup_xend(self, kill):
         return self.cleanup_process(XEND_PID_FILE, "xend", kill)
 
-    def cleanup(self, kill=False):
-        self.cleanup_xend(kill=kill)
-
     def status(self):
         """Returns the status of the xend daemon.
         The return value is defined by the LSB:
@@ -156,16 +125,13 @@ class Daemon:
         # Detach from TTY.
         os.setsid()
 
-        # Detach from standard file descriptors.
-        # I do this at the file-descriptor level: the overlying Python file
-        # objects also use fd's 0, 1 and 2.
+        # Detach from standard file descriptors, and redirect them to
+        # /dev/null or the log as appropriate.
         os.close(0)
         os.close(1)
         os.close(2)
         if XEND_DEBUG:
             os.open('/dev/null', os.O_RDONLY)
-            # XXX KAF: Why doesn't this capture output from C extensions that
-            # fprintf(stdout) or fprintf(stderr) ??
             os.open(XEND_DEBUG_LOG, os.O_WRONLY|os.O_CREAT)
             os.dup(1)
         else:
@@ -180,7 +146,7 @@ class Daemon:
         0  Success
         4  Insufficient privileges
         """
-        xend_pid = self.cleanup_xend()
+        xend_pid = self.cleanup_xend(False)
 
         if self.set_user():
             return 4
@@ -294,7 +260,10 @@ class Daemon:
             return 1
 
     def stop(self):
-        return self.cleanup(kill=True)
+        result = self.cleanup_xend(True)
+        from xen.xend import Vifctl
+        Vifctl.network("stop")
+        return result
 
     def run(self, status):
         try:
index 5de3d332546da8830615e3d217b9346b437ccbe4..012e29cea8c3a71fd87f76c861fb4133c88b7b3f 100644 (file)
@@ -176,7 +176,7 @@ u32 varp_mcast_addr = 0;
 /** UDP port (network order). */
 u16 varp_port = 0;
 
-char *varp_device = "xen-br0";
+char *varp_device = "xenbr0";
 
 #define VarpTable_read_lock(z, flags)    do{ (flags) = 0; down(&(z)->lock); } while(0)
 #define VarpTable_read_unlock(z, flags)  do{ (flags) = 0; up(&(z)->lock); } while(0)